<#--模板拓展,有需求时加入到对应模板即可-->

<#-- *****************************************************Dao.ftl拓展********************************************************* -->
<!-- 批量添加全部所有字段 -->
<insert id="batchInsert${content.content.tableName?cap_first}" parameterType="ArrayList">
    insert into
    ${assign_TableName}
    (<#list content.content.fields as item>${item.name}<#if item?has_next>,</#if></#list>)
    values
    <foreach collection="list" item="item" index="index" separator=",">
        (<#list content.content.fields as item>${r'#{item.'}${item.fieldName}${r'}'}<#if item?has_next>,</#if></#list>)
    </foreach>
</insert>


<!-- 实体类的返回map-->
<resultMap id="BaseResultMap" type="${assign_EntityFullName}">
    <#list content.content.fields as item>
        <#if item.primary == true>
            <id column="${item.name}" property="${item.fieldName}" />
        <#else>
            <result column="${item.name}" property="${item.fieldName}" />
        </#if>
    </#list>
</resultMap>

<!-- 插入一个对象,如果该对象不存在就新建如果该对象已经存在就更新(mysql) -->
<insert id="replace${tableName?cap_first}" parameterType="${assign_EntityFullName}">
    replace into
    ${assign_TableName}
    (<#list content.content.fields as item>${item.name}<#if item?has_next>,</#if></#list>)
    values(<#list content.content.fields as item>${r'#{'}${item.fieldName}${r'}'}<#if item?has_next>,</#if></#list>)
</insert>

<#--不判空插入-->
<!-- 插入一个对象包括属性值为null的值 -->
<insert id="insert${tableName?cap_first}All" parameterType="${assign_EntityFullName}">
    insert into
    ${assign_TableName}
    (<#list content.content.fields as item>${item.name}<#if item?has_next>,</#if></#list>)
    values(<#list content.content.fields as item>${r'#{'}${item.fieldName}${r'}'}<#if item?has_next>,</#if></#list>)
</insert>

<!-- 插入一个对象,只插入对象中值不为null的属性 -->
<insert id="insert${tableName?cap_first}" parameterType="${assign_EntityFullName}">
    insert into ${assign_TableName}
    <trim prefix="(" suffix=")" suffixOverrides=",">
        <#list content.content.fields as item>
            <if test="${item.fieldName} != null and ${item.fieldName} != ''">${item.name}<#if item?has_next>,</#if></if>
        </#list>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
        <#list content.content.fields as item>
            <if test="${item.fieldName} != null and ${item.fieldName} != ''">${r'#{'}${item.fieldName}${r'}'}<#if item?has_next>,</#if></if>
        </#list>
    </trim>
</insert>


<#--不判空更新-->
<!-- 更新一个对象中所有的属性包括null值 -->
<update id="update${tableName?cap_first}All" parameterType="${assign_EntityFullName}">
    update ${assign_TableName}
    <set>
        <#list content.content.fields as item>
            ${item.name} = ${r'#{'}${item.fieldName}${r'}'}<#if item?has_next>,</#if>
        </#list>
    </set>
</update>

<!-- 更新一个对象中属性不为null值,条件为对象中的id -->
<update id="update${tableName?cap_first}" parameterType="${assign_EntityFullName}">
    update ${assign_TableName}
    <set>
        <#list content.content.fields as item>
            <#if item.primary != true>
                <if test="${item.fieldName} != null ">${item.name} = ${r'#{'}${item.fieldName}${r'}'}<#if item?has_next>,</#if></if>
            </#if>
        </#list>
    </set>
    where ${assign_idName} = ${r'#{'}${assign_idFieldName}${r'}'}
</update>


<!-- 通过id删除数据 -->
<delete id="delete${tableName?cap_first}ById" parameterType="${assign_idFieldType}">
    delete from ${assign_TableName}
    where ${assign_idName} = ${r'#{'}${assign_idFieldName}${r'}'}
</delete>


<!--获取数据总行数 -->
<select id="getCount" parameterType="${assign_EntityFullName}" resultType="long">
    select count(*) from ${assign_TableName} t
    <where>
        <#list content.content.fields as item>
        <#if item.primary != true>
        <if test="${item.fieldName} != null and ${item.fieldName} != ''"> and t.${item.name} = ${r'#{'}${item.fieldName}${r'}'}</if>
        </#if>
        </#list>
        <#list content.content.fields as item>
        <#if item.primary == true>
        <if test="${item.fieldName} != null and ${item.fieldName} != ''"> and t.${item.name} = ${r'#{'}${item.fieldName}${r'}'}</if>
        </#if>
        </#list>
    </where>
</select>

